Load libraries

library(tidyverse) # data manipulation
library(ggpubr) # producing data exploratory plots
library(modelsummary) # descriptive data 
library(glmmTMB) # running generalised mixed models 
library(DHARMa) # model diagnostics 
library(performance) # model diagnostics  
library(ggeffects) # partial effect plots 
library(car) # running Anova on model 
library(emmeans) # post-hoc analysis 

Import data

m1 <- read_csv("import_data/1_month_size_data_2022_2023.csv") |> 
  mutate(across(1:15,factor)) |> 
  mutate(STANDARD_LENGTH =LENGTH, 
         .keep = "unused") |> 
  select(!(NOTES)) |> 
  select(1:15,"STANDARD_LENGTH","MASS") |> 
  group_by(CLUTCH_NUMBER) |> 
  mutate(DENSITY = n()) |> 
  ungroup()
           
m2 <- read_csv("import_data/2_month_size_data_2022_2023.csv") |> 
  mutate(across(1:15,factor)) |> 
  mutate(STANDARD_LENGTH=LENGTH, 
         .keep = "unused") |> 
  select(!(NOTES)) |> 
  select(1:15,"STANDARD_LENGTH","MASS")|> 
  group_by(CLUTCH_NUMBER) |> 
  mutate(DENSITY = n()) |> 
  ungroup()

m2.5 <- read_csv("import_data/2-5_month_size_data_2022_2023.csv") |> 
  mutate(across(1:15,factor)) |> 
  mutate(STANDARD_LENGTH =LENGTH, 
         .keep = "unused") |> 
  select(!(NOTES)) |> 
  select(1:15,"STANDARD_LENGTH","MASS")|> 
  group_by(CLUTCH_NUMBER) |> 
  mutate(DENSITY = n()) |> 
  ungroup()

adult <- read_csv("import_data/adult_size_2022_2023.csv") |> 
  mutate(across(1:3,factor), 
         MALE = FISH_ID, 
         FEMALE = FISH_ID, 
         POPULATION = str_sub(FISH_ID, 2,4), 
         POPULATION = case_when(POPULATION == "ARL" ~ "Arlington Reef", 
                                POPULATION == "SUD" ~ "Sudbury Reef",
                                POPULATION == "VLA" ~ "Vlassof cay",
                                POPULATION == "PRE" ~ "Pretty patches", 
                                TRUE ~ POPULATION)) |> 
  left_join(select(m1, c("MALE","TEMPERATURE")), 
             by="MALE") |> 
  left_join(select(m1, c("FEMALE","TEMPERATURE")), 
             by="FEMALE") |>
  distinct() |> 
  mutate(TEMPERATURE = coalesce(TEMPERATURE.x, TEMPERATURE.y)) |> 
  drop_na(TEMPERATURE) |> 
  select(-c("TEMPERATURE.x","TEMPERATURE.y"))

Data manipulation

m1_df <- m1 |> 
  left_join(select(adult, c("MALE", "SL", "MASS")), 
            by ="MALE") |> 
  mutate(SL_MALE =SL, 
         MASS_MALE =MASS.y, 
         .keep = "unused") |>
  left_join(select(adult, c("FEMALE", "SL", "MASS")), 
            by ="FEMALE") |> 
  mutate(SL_FEMALE =SL, 
         MASS_FEMALE =MASS, 
         .keep ="unused") |> 
  mutate(SL_MIDPOINT = (SL_MALE+SL_FEMALE)/2, 
         MASS_MIDPOINT = (MASS_MALE+MASS_FEMALE)/2) |> 
  group_by(CLUTCH_NUMBER) |> 
  mutate(MEDIAN_MASS = median(MASS.x)) |> 
  drop_na(MEDIAN_MASS) |>
  ungroup() |> 
  select(-c("STANDARD_LENGTH","MASS.x", "SAMPLE_NO")) |> 
  distinct() |> 
  mutate(MASS_MIDPOINT =coalesce(MASS_MIDPOINT, MASS_MALE), 
         SL_MIDPOINT =coalesce(SL_MIDPOINT, SL_MALE))

m2_df <- m2 |> 
  left_join(select(adult, c("MALE", "SL", "MASS")), 
            by ="MALE") |> 
  mutate(SL_MALE =SL, 
         MASS_MALE =MASS.y, 
         .keep = "unused") |>
  left_join(select(adult, c("FEMALE", "SL", "MASS")), 
            by ="FEMALE") |> 
  mutate(SL_FEMALE =SL, 
         MASS_FEMALE =MASS, 
         .keep ="unused") |> 
  mutate(SL_MIDPOINT = (SL_MALE+SL_FEMALE)/2, 
         MASS_MIDPOINT = (MASS_MALE+MASS_FEMALE)/2) |> 
  group_by(CLUTCH_NUMBER) |> 
  mutate(MEDIAN_MASS = median(MASS.x)) |> 
  drop_na(MEDIAN_MASS) |>
  ungroup() |> 
  select(-c("STANDARD_LENGTH","MASS.x", "SAMPLE_NO")) |> 
  distinct() |> 
  mutate(MASS_MIDPOINT =coalesce(MASS_MIDPOINT, MASS_MALE), 
         SL_MIDPOINT =coalesce(SL_MIDPOINT, SL_MALE))

m2.5_df <- m2.5 |> 
  left_join(select(adult, c("MALE", "SL", "MASS")), 
            by ="MALE") |> 
  mutate(SL_MALE =SL, 
         MASS_MALE =MASS.y, 
         .keep = "unused") |>
  left_join(select(adult, c("FEMALE", "SL", "MASS")), 
            by ="FEMALE") |> 
  mutate(SL_FEMALE =SL, 
         MASS_FEMALE =MASS, 
         .keep ="unused") |> 
  mutate(SL_MIDPOINT = (SL_MALE+SL_FEMALE)/2, 
         MASS_MIDPOINT = (MASS_MALE+MASS_FEMALE)/2) |> 
  group_by(CLUTCH_NUMBER) |> 
  mutate(MEDIAN_MASS = median(MASS.x)) |> 
  drop_na(MEDIAN_MASS) |>
  ungroup() |> 
  select(-c("STANDARD_LENGTH","MASS.x")) |> 
  distinct() |> 
  mutate(MASS_MIDPOINT =coalesce(MASS_MIDPOINT, MASS_MALE), 
         SL_MIDPOINT =coalesce(SL_MIDPOINT, SL_MALE))|> 
  drop_na(MASS_MALE)

Exploratory data analysis

MASS

plot1 <- ggplot(m1_df, aes(x=MASS_MALE, y=MEDIAN_MASS, color=TEMPERATURE)) +
  geom_point(alpha=0.05) + 
  stat_smooth(method = "lm") +
  ylim(0,0.15) +
  theme_classic()

plot2 <- ggplot(m1_df, aes(x=MASS_FEMALE, y=MEDIAN_MASS, color=TEMPERATURE)) +
  geom_point(alpha=0.05) + 
  stat_smooth(method = "lm") + 
  ylim(0,0.15) +
  theme_classic()

plot3 <- ggplot(m1_df, aes(x=MASS_MIDPOINT, y=MEDIAN_MASS, color=TEMPERATURE)) +
  geom_point(alpha=0.05) + 
  stat_smooth(method = "lm") + 
  ylim(0,0.15) +
  theme_classic()

ggarrange(plot1, plot2, plot3, 
          nrow =1, 
          ncol =3, 
          common.legend = TRUE)

Descriptive statistics

counts

Adults

datasummary(Factor(POPULATION) ~ Factor(TEMPERATURE), 
            data=adult, 
            fmt = "%.0f")
tinytable_uzzbir5e37w8xl6pol53
POPULATION 27 28.5 30
Arlington Reef 8 8 8
Pretty patches 4 6 6
Sudbury Reef 4 4 2
Vlassof cay 6 2 6

1-months

datasummary(Factor(POPULATION) ~ Factor(TEMPERATURE), 
            data=m1_df, 
            fmt = "%.0f")
tinytable_63x5zsxzfjyebgn0cei2
POPULATION 27 28.5 30
Arlington Reef 8 4 7
Pretty Patches 4 3 4
Sudbury Reef 4 2 2
Vlassof Cay 4 1 4

2-months

datasummary(Factor(POPULATION) ~ Factor(TEMPERATURE), 
            data=m2_df, 
            fmt = "%.0f")
tinytable_wjxfwpg8bwwuwef66kxz
POPULATION 27 28.5 30
Arlington Reef 7 4 6
Pretty Patches 4 3 5
Sudbury Reef 4 2 1
Vlassof Cay 3 3 4

2.5-months

datasummary(Factor(POPULATION) ~ Factor(TEMPERATURE), 
            data=m2.5_df, 
            fmt = "%.0f")
tinytable_c54ibj1tl1iizt033fzm
POPULATION 27 28.5 30
Arlington Reef 8 5 8
Pretty Patches 4 3 4
Sudbury Reef 3 2 2
Vlassof Cay 3 2 4

mass

Adults

datasummary(Factor(TEMPERATURE) ~ MASS * (NUnique + mean + median + min + max + sd + Histogram), 
            data = drop_na(adult, MASS),  
            fmt = "%.2f")
tinytable_4y3hpffxisufhqcvvjlz
TEMPERATURE NUnique mean median min max sd Histogram
27 21 46.93 50.63 29.85 59.93 10.30 ▇▁▃▄▄▆▄
28.5 20 38.81 42.36 16.28 53.26 10.26 ▁▁▄▃▄▇▆▁
30 22 39.94 39.62 23.91 57.31 9.43 ▅▇▂▇▇▇▂▇▅▂

1-months

datasummary(Factor(TEMPERATURE) ~ MEDIAN_MASS * (NUnique + mean + median + min + max + sd + Histogram), 
            data = drop_na(m1_df, MEDIAN_MASS),  
            fmt = "%.2f")
tinytable_f9vf8nfvat5v8030u4yt
TEMPERATURE NUnique mean median min max sd Histogram
27 20 0.06 0.06 0.02 0.12 0.02 ▁▂▂▇▅▁▂▁▁
28.5 10 0.07 0.06 0.04 0.09 0.02 ▃▃▃▇▇▃▇
30 17 0.07 0.06 0.04 0.11 0.02 ▅▂▇▇▇▅▂▅

2-months

datasummary(Factor(TEMPERATURE) ~ MEDIAN_MASS * (NUnique + mean + median + min + max + sd + Histogram), 
            data = drop_na(m2_df, MEDIAN_MASS),  
            fmt = "%.2f")
tinytable_3ot9ao23kes2v4fgiss1
TEMPERATURE NUnique mean median min max sd Histogram
27 18 0.27 0.27 0.15 0.36 0.05 ▁▁▁▄▇▁▆▁▁
28.5 11 0.28 0.27 0.21 0.34 0.03 ▂▂▅▇▂▂▂▂▂
30 16 0.28 0.29 0.19 0.37 0.05 ▃▂▅▇▇▂▂

2.5-months

datasummary(Factor(TEMPERATURE) ~ MEDIAN_MASS * (NUnique + mean + median + min + max + sd + Histogram), 
            data = drop_na(m2.5_df, MEDIAN_MASS),  
            fmt = "%.2f")
tinytable_r4ywfwfxbxbho1oa4tsu
TEMPERATURE NUnique mean median min max sd Histogram
27 18 0.48 0.47 0.27 0.70 0.10 ▂▃▅▅▇▅▂▂
28.5 12 0.47 0.43 0.33 0.77 0.15 ▇▅▅▅▂▅
30 18 0.45 0.44 0.37 0.55 0.05 ▅▂▂▇▅▂▃▃▂

Fit models [random factors]

1-month

modelNULL <- glmmTMB(MEDIAN_MASS ~ 1, 
                  family=gaussian(),
                  data =m1_df)

model2 <- glmmTMB(MEDIAN_MASS ~ (1|LEVEL), 
                  family=gaussian(),
                  data = m1_df)

model3 <- glmmTMB(MEDIAN_MASS ~ (1|CLUTCH_ORDER), 
                  family=gaussian(),
                  data = m1_df)

model4 <- glmmTMB(MEDIAN_MASS ~ (1|REGION), 
                  family=gaussian(),
                  data = m1_df) 

model5 <- glmmTMB(MEDIAN_MASS ~ (1|REGION) + (1|POPULATION), 
                  family=gaussian(),
                  data = m1_df) 
## Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
## problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')
model6 <- glmmTMB(MEDIAN_MASS ~ (1|CLUTCH_ORDER) + (1|REGION) + (1|POPULATION), 
                  family=gaussian(),
                  data = m1_df) 

AIC(modelNULL, model2, model3, model4, model5, model6) 
BIC(modelNULL, model2, model3, model4, model5, model6)

2-month

modelNULL <- glmmTMB(MEDIAN_MASS ~ 1, 
                  family=gaussian(),
                  data =m2_df)

model2 <- glmmTMB(MEDIAN_MASS ~ (1|LEVEL), 
                  family=gaussian(),
                  data = m2_df)

model3 <- glmmTMB(MEDIAN_MASS ~ (1|CLUTCH_ORDER), 
                  family=gaussian(),
                  data = m2_df)

model4 <- glmmTMB(MEDIAN_MASS ~ (1|REGION), 
                  family=gaussian(),
                  data = m2_df) 

model5 <- glmmTMB(MEDIAN_MASS ~ (1|REGION) + (1|POPULATION), 
                  family=gaussian(),
                  data = m2_df) 

model6 <- glmmTMB(MEDIAN_MASS ~ (1|CLUTCH_ORDER) + (1|REGION) + (1|POPULATION), 
                  family=gaussian(),
                  data = m2_df) 

AIC(modelNULL, model2, model3, model4, model5, model6) 
BIC(modelNULL, model2, model3, model4, model5, model6)

2.5-month

modelNULL <- glmmTMB(MEDIAN_MASS ~ 1, 
                  family=gaussian(),
                  data =m2.5_df)

model2 <- glmmTMB(MEDIAN_MASS ~ (1|LEVEL), 
                  family=gaussian(),
                  data = m2.5_df)

model3 <- glmmTMB(MEDIAN_MASS ~ (1|CLUTCH_ORDER), 
                  family=gaussian(),
                  data = m2.5_df)

model4 <- glmmTMB(MEDIAN_MASS ~ (1|REGION), 
                  family=gaussian(),
                  data = m2.5_df) 

model5 <- glmmTMB(MEDIAN_MASS ~ (1|REGION) + (1|POPULATION), 
                  family=gaussian(),
                  data = m2.5_df) 
## Warning in finalizeTMB(TMBStruc, obj, fit, h, data.tmb.old): Model convergence
## problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')
model6 <- glmmTMB(MEDIAN_MASS ~ (1|CLUTCH_ORDER) + (1|REGION) + (1|POPULATION), 
                  family=gaussian(),
                  data = m2.5_df) 

AIC(modelNULL, model2, model3, model4, model5, model6) 
BIC(modelNULL, model2, model3, model4, model5, model6)

For mass measurements at different time periods, including 1, 2, and 2.5 months the best model is the most simply model (i.e., model1), where the only random factor that is present is CLUTCH_NUMBER.

Fit model [fixed factors]

Now that we have figured out which random factors will be included within out generalized linear mixed effects model we can start to explore different hypothesese by adding in our fixed factors - covariates.

Fixed factors that will be included will be those that are essential to answering the initial research question based on heiritability of traits between offspring and parental fish - labelled as MALE and FEMALE in the dataframe as well as their combined score MIDPOINT, if applicable. TEMPERATURE is also essential to answering the main research question that looks to see if heritability changes at different temperatures.

Our main research hypothesis will be modelled using the formula below”

MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE

An alternative research hypothesis will will test will include an interaction with PARENTAL_DAYS_IN_TEMPERATURE to see if heritability was affect by how long adults spent at experimental temperatures. This model may look something like:

MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE:PARENTAL_DAYS_IN_TREATMENT

Lets start fitting models:

offspring-male

1-month

Models

Main hypothesis
model1a <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE+ scale(DENSITY), 
                    family='gaussian', 
                    data=m1_df)
Alternative hypothesis
model1b <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE*scale(as.numeric(PARENTAL_DAYS_IN_TREATMENT))+ scale(DENSITY), 
                    family='gaussian', 
                    data=m1_df)

Model selection

AIC(model1a, model1b, k=12) 
BIC(model1a, model1b)

Model1a was selected as the best model and will be used going forward.

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.5 0.264 0.856 0.284 0.232 0.952 0.876 0.096 0.144 0.888 1 0.06 0.436 0.072 0.904 0.296 0.196 0.504 0.552 0.424 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.12213, p-value = 0.4849
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0221, p-value = 0.872
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 47, p-value = 0.3134
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005385317 0.1129377171
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                              0.0212766
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.12213, p-value = 0.4849
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0221, p-value = 0.872
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 47, p-value = 0.3134
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005385317 0.1129377171
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                              0.0212766
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Partial effect plots

model1a |> ggemmeans(~MASS_MALE|TEMPERATURE) |> 
  plot(add.data =FALSE) 

model1a |> ggemmeans(~TEMPERATURE) |> 
  plot(add.data =FALSE) 
## NOTE: Results may be misleading due to involvement in interactions

Model investigation

Summary
model1a |> summary()
##  Family: gaussian  ( identity )
## Formula:          MEDIAN_MASS ~ scale(MASS_MALE) * TEMPERATURE + scale(DENSITY)
## Data: m1_df
## 
##      AIC      BIC   logLik deviance df.resid 
##   -221.8   -207.0    118.9   -237.8       39 
## 
## 
## Dispersion estimate for gaussian family (sigma^2): 0.000372 
## 
## Conditional model:
##                                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                       0.0578168  0.0048851  11.835   <2e-16 ***
## scale(MASS_MALE)                  0.0058811  0.0051837   1.135   0.2566    
## TEMPERATURE28.5                   0.0088536  0.0081615   1.085   0.2780    
## TEMPERATURE30                     0.0125701  0.0070335   1.787   0.0739 .  
## scale(DENSITY)                    0.0001506  0.0031777   0.047   0.9622    
## scale(MASS_MALE):TEMPERATURE28.5 -0.0042435  0.0089813  -0.472   0.6366    
## scale(MASS_MALE):TEMPERATURE30   -0.0034793  0.0070392  -0.494   0.6211    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova
model1a |> Anova()
Confint
model1a |> confint()
##                                         2.5 %      97.5 %      Estimate
## (Intercept)                       0.048242264 0.067391341  0.0578168021
## scale(MASS_MALE)                 -0.004278816 0.016041083  0.0058811338
## TEMPERATURE28.5                  -0.007142549 0.024849787  0.0088536190
## TEMPERATURE30                    -0.001215362 0.026355474  0.0125700562
## scale(DENSITY)                   -0.006077575 0.006378709  0.0001505672
## scale(MASS_MALE):TEMPERATURE28.5 -0.021846483 0.013359409 -0.0042435370
## scale(MASS_MALE):TEMPERATURE30   -0.017275889 0.010317287 -0.0034793010
r-squared
model1a |> r2_nakagawa()
## Random effect variances not available. Returned R2 does not account for random effects.
## # R2 for Mixed Models
## 
##   Conditional R2: NA
##      Marginal R2: 0.081

Summary figure

m1.mass <- emmeans(model1a, ~ MASS_MALE*TEMPERATURE, 
                 at =list(MASS_MALE=seq(from =min(m1_df$MASS_MALE), to =max(m1_df$MASS_MALE), by=.25)))

m1.mass.df <- as.data.frame(m1.mass)

m1.mass.obs <- drop_na(m1_df, MASS_MALE, MEDIAN_MASS) |> 
  mutate(Pred =predict(model1a, re.form=NA, type ='response'), 
         Resid =residuals(model1a, type ='response'), 
         Fit =Pred+Resid) 

m1.mass.obs.summarize <-  m1.mass.obs |> 
  group_by(CLUTCH_NUMBER, TEMPERATURE) |> 
  summarise(mean.mass =mean(Fit, na.rm=TRUE), 
            mean.mass.male =mean(MASS_MALE, na.rm = TRUE), 
            sd.mass =sd(Fit, na.rm =TRUE), 
            n.mass = n()) |> 
  mutate(se.mass = sd.mass / sqrt(n.mass), 
         lower.ci.mass =mean.mass - qt(1-(0.05/2), n.mass -1) * se.mass, 
         upper.ci.mass =mean.mass + qt(1-(0.05/2), n.mass -1) * se.mass) |> 
  ungroup()
## `summarise()` has grouped output by 'CLUTCH_NUMBER'. You can override using the
## `.groups` argument.
## Warning: There were 94 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.mass = mean.mass - qt(1 - (0.05/2), n.mass - 1) *
##   se.mass`.
## ℹ In group 1: `CLUTCH_NUMBER = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 93 remaining warnings.
ggplot(data = m1.mass.df, aes(x=MASS_MALE, y=emmean)) + 
  stat_smooth(aes(color=TEMPERATURE), 
              method = "lm") + 
  geom_pointrange(data = m1.mass.obs.summarize, aes(x =mean.mass.male, 
                                                  y =mean.mass, 
                                                  ymin =lower.ci.mass, 
                                                  ymax =upper.ci.mass, 
                                                  color = TEMPERATURE)) +  
  scale_color_manual(values = c("#69d7d8","#ff9c56", "#903146")) + 
  scale_fill_manual(values =c("#69d7d8","#ff9c56", "#903146")) +
  facet_wrap(~TEMPERATURE)+
  xlab("PARENTAL MALE MASS (g)") + 
  ylab("OFFSPRING MASS (g)") + 
  ggtitle("Offspring-male relationship") +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_segment()`).

2-month

Models

Main hypothesis
model1a <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE+ scale(DENSITY), 
                    family=gaussian(), 
                    data=m2_df)
Alternative hypothesis
model1b <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE*scale(as.numeric(PARENTAL_DAYS_IN_TREATMENT))+ scale(DENSITY), 
                    family=gaussian(), 
                    data=m2_df)

Model selection

AIC(model1a, model1b, k=12) 
BIC(model1a, model1b)

The null model appears better than the models that we used. Let’s explore the data bit more and see if we can find a reason for this. Let’s start by looking at a basic histogram of our data.

hist(m2_df$MEDIAN_MASS)

There appears to be a left skew within our data. Let’s see if this can be better modelled with a Gamma distribution. If not we can try to incorporate transformations to our response variable. The model validations below also could use some improving.

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.452 0 0.204 0.876 0.564 0.788 0.128 0.824 0.616 0.748 0.96 0.836 0.96 0.856 0.636 0.956 0.32 0.432 0.668 0.472 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.080348, p-value = 0.9278
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 46, p-value = 0.3079
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005502357 0.1152718256
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                             0.02173913
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.080348, p-value = 0.9278
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 46, p-value = 0.3079
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005502357 0.1152718256
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                             0.02173913
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Partial effect plots

model1a |> ggemmeans(~MASS_MALE|TEMPERATURE) |> 
  plot(add.data =FALSE) 

model1a |> ggemmeans(~TEMPERATURE) |> 
  plot(add.data =FALSE) 
## NOTE: Results may be misleading due to involvement in interactions

Model investigation

Summary
model1a |> summary()
##  Family: gaussian  ( identity )
## Formula:          MEDIAN_MASS ~ scale(MASS_MALE) * TEMPERATURE + scale(DENSITY)
## Data: m2_df
## 
##      AIC      BIC   logLik deviance df.resid 
##   -148.4   -133.7     82.2   -164.4       38 
## 
## 
## Dispersion estimate for gaussian family (sigma^2): 0.00164 
## 
## Conditional model:
##                                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                       0.270381   0.011673  23.162   <2e-16 ***
## scale(MASS_MALE)                 -0.002550   0.011895  -0.214   0.8303    
## TEMPERATURE28.5                   0.010136   0.018309   0.554   0.5799    
## TEMPERATURE30                     0.005808   0.015780   0.368   0.7128    
## scale(DENSITY)                   -0.015049   0.007144  -2.107   0.0352 *  
## scale(MASS_MALE):TEMPERATURE28.5 -0.003736   0.019757  -0.189   0.8500    
## scale(MASS_MALE):TEMPERATURE30    0.004556   0.014829   0.307   0.7587    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova
model1a |> Anova()
Confint
model1a |> confint()
##                                        2.5 %       97.5 %     Estimate
## (Intercept)                       0.24750182  0.293260769  0.270381294
## scale(MASS_MALE)                 -0.02586306  0.020763890 -0.002549585
## TEMPERATURE28.5                  -0.02574975  0.046021385  0.010135819
## TEMPERATURE30                    -0.02512076  0.036737207  0.005808224
## scale(DENSITY)                   -0.02905035 -0.001047521 -0.015048934
## scale(MASS_MALE):TEMPERATURE28.5 -0.04245986  0.034987351 -0.003736256
## scale(MASS_MALE):TEMPERATURE30   -0.02450841  0.033619677  0.004555633
r-squared
model1a |> r2_nakagawa()
## Random effect variances not available. Returned R2 does not account for random effects.
## # R2 for Mixed Models
## 
##   Conditional R2: NA
##      Marginal R2: 0.124

Summary figure

m2.mass <- emmeans(model1a, ~ MASS_MALE*TEMPERATURE, 
                 at =list(MASS_MALE=seq(from =min(m2_df$MASS_MALE), to =max(m2_df$MASS_MALE), by=.25)))

m2.mass.df <- as.data.frame(m2.mass)

m2.mass.obs <- drop_na(m2_df, MASS_MALE, MEDIAN_MASS) |> 
  mutate(Pred =predict(model1a, re.form=NA, type ='response'), 
         Resid =residuals(model1a, type ='response'), 
         Fit =Pred+Resid) 

m2.mass.obs.summarize <-  m2.mass.obs |> 
  group_by(CLUTCH_NUMBER, TEMPERATURE) |> 
  summarise(mean.mass =mean(Fit, na.rm=TRUE), 
            mean.mass.male =mean(MASS_MALE, na.rm = TRUE), 
            sd.mass =sd(Fit, na.rm =TRUE), 
            n.mass = n()) |> 
  mutate(se.mass = sd.mass / sqrt(n.mass), 
         lower.ci.mass =mean.mass - qt(1-(0.05/2), n.mass -1) * se.mass, 
         upper.ci.mass =mean.mass + qt(1-(0.05/2), n.mass -1) * se.mass) |> 
  ungroup()
## `summarise()` has grouped output by 'CLUTCH_NUMBER'. You can override using the
## `.groups` argument.
## Warning: There were 88 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.mass = mean.mass - qt(1 - (0.05/2), n.mass - 1) *
##   se.mass`.
## ℹ In group 1: `CLUTCH_NUMBER = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 87 remaining warnings.
ggplot(data = m2.mass.df, aes(x=MASS_MALE, y=emmean)) + 
  stat_smooth(aes(color=TEMPERATURE), 
              method = "lm", 
              formula = y ~ x) + 
  geom_pointrange(data = m2.mass.obs.summarize, aes(x =mean.mass.male, 
                                                  y =mean.mass, 
                                                  ymin =lower.ci.mass, 
                                                  ymax =upper.ci.mass, 
                                                  color = TEMPERATURE)) +  
  scale_color_manual(values = c("#69d7d8","#ff9c56", "#903146")) + 
  scale_fill_manual(values =c("#69d7d8","#ff9c56", "#903146")) +
  facet_wrap(~TEMPERATURE)+
  xlab("PARENTAL MALE STANDARD LENGTH (mm)") + 
  ylab("OFFSPRING STANDARD LENGTH (mm)") + 
  ggtitle("Offspring-male relationship") +
  theme_classic()
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 16 rows containing missing values or values outside the scale range
## (`geom_segment()`).

2.5-month

Models

Main hypothesis
model1a <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE+ scale(DENSITY), 
                    family=gaussian(link ="identity"), 
                    data=m2.5_df)
Alternative hypothesis
model1b <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MALE)*TEMPERATURE*scale(as.numeric(PARENTAL_DAYS_IN_TREATMENT))+ scale(DENSITY), 
                    family=gaussian(link ="identity"), 
                    data=m2.5_df)

Model selection

AIC(model1a, model1b, k=13) 
BIC(model1a, model1b)

Once again the NULL model seems to outperform our hypothesis testing models. Let’s follow the steps that we conducted for 2-month data and appy a log transformation to our dataset to see if it improved the model.

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.988 0.744 0.792 0.156 0.596 0.956 0.776 0.844 0.896 0.568 0.768 0.944 0.68 0.46 0.3 0.952 0.768 0.724 0.42 0.804 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.078833, p-value = 0.9266
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0217, p-value = 0.88
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 0, observations = 48, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.00000000 0.07397279
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                                      0
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.078833, p-value = 0.9266
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0217, p-value = 0.88
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 0, observations = 48, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.00000000 0.07397279
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                                      0
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Partial effect plots

model1a |> ggemmeans(~MASS_MALE|TEMPERATURE) |> 
  plot(add.data =FALSE) 

model1a |> ggemmeans(~TEMPERATURE) |> 
  plot(add.data =FALSE) 
## NOTE: Results may be misleading due to involvement in interactions

Model investigation

Summary
model1a |> summary()
##  Family: gaussian  ( identity )
## Formula:          MEDIAN_MASS ~ scale(MASS_MALE) * TEMPERATURE + scale(DENSITY)
## Data: m2.5_df
## 
##      AIC      BIC   logLik deviance df.resid 
##   -109.0    -94.0     62.5   -125.0       40 
## 
## 
## Dispersion estimate for gaussian family (sigma^2): 0.00433 
## 
## Conditional model:
##                                   Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                       0.471464   0.017449  27.020  < 2e-16 ***
## scale(MASS_MALE)                  0.025374   0.016358   1.551    0.121    
## TEMPERATURE28.5                  -0.020863   0.026902  -0.776    0.438    
## TEMPERATURE30                    -0.014006   0.023848  -0.587    0.557    
## scale(DENSITY)                   -0.072959   0.009758  -7.477 7.63e-14 ***
## scale(MASS_MALE):TEMPERATURE28.5 -0.045901   0.030630  -1.499    0.134    
## scale(MASS_MALE):TEMPERATURE30   -0.013601   0.022733  -0.598    0.550    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova
model1a |> Anova()
Confint
model1a |> confint()
##                                        2.5 %      97.5 %    Estimate
## (Intercept)                       0.43726427  0.50566312  0.47146369
## scale(MASS_MALE)                 -0.00668662  0.05743449  0.02537393
## TEMPERATURE28.5                  -0.07359025  0.03186350 -0.02086337
## TEMPERATURE30                    -0.06074605  0.03273487 -0.01400559
## scale(DENSITY)                   -0.09208455 -0.05383293 -0.07295874
## scale(MASS_MALE):TEMPERATURE28.5 -0.10593556  0.01413293 -0.04590131
## scale(MASS_MALE):TEMPERATURE30   -0.05815793  0.03095547 -0.01360123
r-squared
model1a |> r2_nakagawa()
## Random effect variances not available. Returned R2 does not account for random effects.
## # R2 for Mixed Models
## 
##   Conditional R2: NA
##      Marginal R2: 0.561

Summary figure

m1.mass <- emmeans(model1a, ~ MASS_MALE*TEMPERATURE, 
                 at =list(MASS_MALE=seq(from =min(m2.5_df$MASS_MALE), to =max(m2.5_df$MASS_MALE), by=.25)))

m1.mass.df <- as.data.frame(m1.mass)

m1.mass.obs <- drop_na(m2.5_df, MASS_MALE, MEDIAN_MASS) |> 
  mutate(Pred =predict(model1a, re.form=NA, type ='response'), 
         Resid =residuals(model1a, type ='response'), 
         Fit =Pred+Resid) 

m1.mass.obs.summarize <-  m1.mass.obs |> 
  group_by(CLUTCH_NUMBER, TEMPERATURE) |> 
  summarise(mean.mass =mean(Fit, na.rm=TRUE), 
            mean.mass.male =mean(MASS_MALE, na.rm = TRUE), 
            sd.mass =sd(Fit, na.rm =TRUE), 
            n.mass = n()) |> 
  mutate(se.mass = sd.mass / sqrt(n.mass), 
         lower.ci.mass =mean.mass - qt(1-(0.05/2), n.mass -1) * se.mass, 
         upper.ci.mass =mean.mass + qt(1-(0.05/2), n.mass -1) * se.mass) |> 
  ungroup()
## `summarise()` has grouped output by 'CLUTCH_NUMBER'. You can override using the
## `.groups` argument.
## Warning: There were 96 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.mass = mean.mass - qt(1 - (0.05/2), n.mass - 1) *
##   se.mass`.
## ℹ In group 1: `CLUTCH_NUMBER = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 95 remaining warnings.
ggplot(data = m1.mass.df, aes(x=MASS_MALE, y=emmean)) + 
  stat_smooth(aes(color=TEMPERATURE), 
              method = "lm", 
              formula = y ~ x) + 
  geom_pointrange(data = m1.mass.obs.summarize, aes(x =mean.mass.male, 
                                                  y =mean.mass, 
                                                  ymin =lower.ci.mass, 
                                                  ymax =upper.ci.mass, 
                                                  color = TEMPERATURE)) +  
  scale_color_manual(values = c("#69d7d8","#ff9c56", "#903146")) + 
  scale_fill_manual(values =c("#69d7d8","#ff9c56", "#903146")) +
  facet_wrap(~TEMPERATURE)+
  xlab("PARENTAL MALE MASS (g)") + 
  ylab("MEDIAN MASS (g)") + 
  ggtitle("Offspring-male relationship") +
  theme_classic()
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 12 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_segment()`).

Offspring-Midpoint

1-month

Models

Main hypothesis
model1a <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MIDPOINT)*TEMPERATURE + scale(DENSITY), 
                    family=gaussian(), 
                    data=m1_df)
Alternative hypothesis
model1b <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MIDPOINT)*TEMPERATURE*scale(as.numeric(PARENTAL_DAYS_IN_TREATMENT)) + scale(DENSITY), 
                    family=gaussian(), 
                    data=m1_df)

Model selection

AIC(model1a, model1b, k=12) 
BIC(model1a, model1b)

Model1a was selected as the best model and will be used going forward.

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.504 0.284 0.864 0.264 0.228 0.952 0.852 0.1 0.184 0.884 1 0.052 0.416 0.084 0.912 0.3 0.2 0.496 0.528 0.456 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.12213, p-value = 0.4849
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0221, p-value = 0.872
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 47, p-value = 0.3134
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005385317 0.1129377171
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                              0.0212766
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.12213, p-value = 0.4849
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0221, p-value = 0.872
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 47, p-value = 0.3134
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005385317 0.1129377171
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                              0.0212766
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Partial effect plots

model1a |> ggemmeans(~MASS_MIDPOINT|TEMPERATURE) |> 
  plot(add.data =FALSE) 

model1a |> ggemmeans(~TEMPERATURE) |> 
  plot(add.data =FALSE) 
## NOTE: Results may be misleading due to involvement in interactions

Model investigation

Summary
model1a |> summary()
##  Family: gaussian  ( identity )
## Formula:          
## MEDIAN_MASS ~ scale(MASS_MIDPOINT) * TEMPERATURE + scale(DENSITY)
## Data: m1_df
## 
##      AIC      BIC   logLik deviance df.resid 
##   -221.6   -206.8    118.8   -237.6       39 
## 
## 
## Dispersion estimate for gaussian family (sigma^2): 0.000373 
## 
## Conditional model:
##                                        Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                           0.0587013  0.0046690  12.573   <2e-16 ***
## scale(MASS_MIDPOINT)                  0.0045869  0.0047451   0.967   0.3337    
## TEMPERATURE28.5                       0.0086531  0.0080186   1.079   0.2805    
## TEMPERATURE30                         0.0115693  0.0068154   1.698   0.0896 .  
## scale(DENSITY)                        0.0001254  0.0031975   0.039   0.9687    
## scale(MASS_MIDPOINT):TEMPERATURE28.5 -0.0012688  0.0077652  -0.163   0.8702    
## scale(MASS_MIDPOINT):TEMPERATURE30   -0.0015550  0.0071932  -0.216   0.8288    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova
model1a |> Anova()
Confint
model1a |> confint()
##                                             2.5 %     97.5 %      Estimate
## (Intercept)                           0.049550342 0.06785235  0.0587013451
## scale(MASS_MIDPOINT)                 -0.004713292 0.01388702  0.0045868646
## TEMPERATURE28.5                      -0.007063129 0.02436923  0.0086530523
## TEMPERATURE30                        -0.001788599 0.02492720  0.0115692983
## scale(DENSITY)                       -0.006141489 0.00639231  0.0001254108
## scale(MASS_MIDPOINT):TEMPERATURE28.5 -0.016488358 0.01395083 -0.0012687619
## scale(MASS_MIDPOINT):TEMPERATURE30   -0.015653450 0.01254343 -0.0015550098
r-squared
model1a |> r2_nakagawa()
## Random effect variances not available. Returned R2 does not account for random effects.
## # R2 for Mixed Models
## 
##   Conditional R2: NA
##      Marginal R2: 0.078

Summary figure

m1_df <-  
  m1_df |> 
  drop_na(MASS_MIDPOINT)

m1.mass <- emmeans(model1a, ~ MASS_MIDPOINT*TEMPERATURE, 
                 at =list(MASS_MIDPOINT=seq(from =min(m1_df$MASS_MIDPOINT), to =max(m1_df$MASS_MIDPOINT), by=.25)))

m1.mass.df <- as.data.frame(m1.mass)

m1.mass.obs <- drop_na(m1_df, MASS_MIDPOINT, MEDIAN_MASS) |> 
  mutate(Pred =predict(model1a, re.form=NA, type ='response'), 
         Resid =residuals(model1a, type ='response'), 
         Fit =Pred+Resid) 

m1.mass.obs.summarize <-  m1.mass.obs |> 
  group_by(CLUTCH_NUMBER, TEMPERATURE) |> 
  summarise(mean.mass =mean(Fit, na.rm=TRUE), 
            mean.mass.female =mean(MASS_MIDPOINT, na.rm = TRUE), 
            sd.mass =sd(Fit, na.rm =TRUE), 
            n.mass = n()) |> 
  mutate(se.mass = sd.mass / sqrt(n.mass), 
         lower.ci.mass =mean.mass - qt(1-(0.05/2), n.mass -1) * se.mass, 
         upper.ci.mass =mean.mass + qt(1-(0.05/2), n.mass -1) * se.mass) |> 
  ungroup()
## `summarise()` has grouped output by 'CLUTCH_NUMBER'. You can override using the
## `.groups` argument.
## Warning: There were 94 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.mass = mean.mass - qt(1 - (0.05/2), n.mass - 1) *
##   se.mass`.
## ℹ In group 1: `CLUTCH_NUMBER = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 93 remaining warnings.
ggplot(data = m1.mass.df, aes(x=MASS_MIDPOINT, y=emmean)) + 
  stat_smooth(aes(color=TEMPERATURE), 
              method = "lm") + 
  geom_pointrange(data = m1.mass.obs.summarize, aes(x =mean.mass.female, 
                                                  y =mean.mass, 
                                                  ymin =lower.ci.mass, 
                                                  ymax =upper.ci.mass, 
                                                  color = TEMPERATURE)) +  
  scale_color_manual(values = c("#69d7d8","#ff9c56", "#903146")) + 
  scale_fill_manual(values =c("#69d7d8","#ff9c56", "#903146")) +
  facet_wrap(~TEMPERATURE)+
  xlab("PARENTAL MIDPOINT STANDARD LENGTH (mm)") + 
  ylab("OFFSPRING STANDARD LENGTH (mm)") + 
  ggtitle("Offspring-male relationship") +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 17 rows containing missing values or values outside the scale range
## (`geom_segment()`).

2-month

Models

Main hypothesis
model1a <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MIDPOINT)*TEMPERATURE + scale(DENSITY), 
                    family=gaussian(), 
                    data=m2_df)
Alternative hypothesis
model1b <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MIDPOINT)*TEMPERATURE*scale(as.numeric(PARENTAL_DAYS_IN_TREATMENT)) + scale(DENSITY),
                    family=gaussian(), 
                    data=m2_df)

Model selection

AIC(model1a, model1b, k=12) 
BIC(model1a, model1b)

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  
## Warning in newton(lsp = lsp, X = G$X, y = G$y, Eb = G$Eb, UrS = G$UrS, L = G$L,
## : Fitting terminated with step failure - check results carefully

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.452 0 0.224 0.884 0.568 0.78 0.128 0.892 0.668 0.784 0.96 0.808 0.936 0.856 0.656 0.952 0.32 0.428 0.664 0.604 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.089391, p-value = 0.8559
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 46, p-value = 0.3079
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005502357 0.1152718256
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                             0.02173913
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.089391, p-value = 0.8559
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 46, p-value = 0.3079
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005502357 0.1152718256
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                             0.02173913
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  
## Warning in newton(lsp = lsp, X = G$X, y = G$y, Eb = G$Eb, UrS = G$UrS, L = G$L,
## : Fitting terminated with step failure - check results carefully

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.452 0 0.224 0.884 0.568 0.78 0.128 0.892 0.668 0.784 0.96 0.808 0.936 0.856 0.656 0.952 0.32 0.428 0.664 0.604 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.089391, p-value = 0.8559
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 46, p-value = 0.3079
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005502357 0.1152718256
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                             0.02173913
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.089391, p-value = 0.8559
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0232, p-value = 0.856
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 1, observations = 46, p-value = 0.3079
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.0005502357 0.1152718256
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                             0.02173913
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Partial effect plots

model1a |> ggemmeans(~MASS_MIDPOINT|TEMPERATURE) |> 
  plot(add.data =FALSE) 

model1a |> ggemmeans(~TEMPERATURE) |> 
  plot(add.data =FALSE) 
## NOTE: Results may be misleading due to involvement in interactions

Model investigation

Summary
model1a |> summary()
##  Family: gaussian  ( identity )
## Formula:          
## MEDIAN_MASS ~ scale(MASS_MIDPOINT) * TEMPERATURE + scale(DENSITY)
## Data: m2_df
## 
##      AIC      BIC   logLik deviance df.resid 
##   -148.5   -133.9     82.3   -164.5       38 
## 
## 
## Dispersion estimate for gaussian family (sigma^2): 0.00164 
## 
## Conditional model:
##                                       Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                           0.270698   0.010849  24.951   <2e-16 ***
## scale(MASS_MIDPOINT)                 -0.002989   0.010562  -0.283   0.7772    
## TEMPERATURE28.5                       0.010798   0.017496   0.617   0.5371    
## TEMPERATURE30                         0.003479   0.015047   0.231   0.8172    
## scale(DENSITY)                       -0.013287   0.007221  -1.840   0.0658 .  
## scale(MASS_MIDPOINT):TEMPERATURE28.5  0.001755   0.016633   0.105   0.9160    
## scale(MASS_MIDPOINT):TEMPERATURE30   -0.004617   0.015067  -0.306   0.7593    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova
model1a |> Anova()
Confint
model1a |> confint()
##                                            2.5 %       97.5 %     Estimate
## (Intercept)                           0.24943399 0.2919622252  0.270698107
## scale(MASS_MIDPOINT)                 -0.02368984 0.0177121013 -0.002988872
## TEMPERATURE28.5                      -0.02349337 0.0450889225  0.010797777
## TEMPERATURE30                        -0.02601319 0.0329705215  0.003478665
## scale(DENSITY)                       -0.02743873 0.0008655102 -0.013286607
## scale(MASS_MIDPOINT):TEMPERATURE28.5 -0.03084546 0.0343545490  0.001754542
## scale(MASS_MIDPOINT):TEMPERATURE30   -0.03414709 0.0249129745 -0.004617056
r-squared
model1a |> r2_nakagawa()
## Random effect variances not available. Returned R2 does not account for random effects.
## # R2 for Mixed Models
## 
##   Conditional R2: NA
##      Marginal R2: 0.127

Summary figure

m2_df <-  
  m2_df |> 
  drop_na(MASS_MIDPOINT)
m2.mass <- emmeans(model1a, ~ MASS_MIDPOINT*TEMPERATURE, 
                 at =list(MASS_MIDPOINT=seq(from =min(m2_df$MASS_MIDPOINT), to =max(m2_df$MASS_MIDPOINT), by=.25)))

m2.mass.df <- as.data.frame(m2.mass)

m2.mass.obs <- drop_na(m2_df, MASS_MIDPOINT,MEDIAN_MASS) |> 
  mutate(Pred =predict(model1a, re.form=NA, type ='response'), 
         Resid =residuals(model1a, type ='response'), 
         Fit =Pred+Resid) 

m2.mass.obs.summarize <-  m2.mass.obs |> 
  group_by(CLUTCH_NUMBER, TEMPERATURE) |> 
  summarise(mean.mass =mean(Fit, na.rm=TRUE), 
            mean.mass.male =mean(MASS_MIDPOINT, na.rm = TRUE), 
            sd.mass =sd(Fit, na.rm =TRUE), 
            n.mass = n()) |> 
  mutate(se.mass = sd.mass / sqrt(n.mass), 
         lower.ci.mass =mean.mass - qt(1-(0.05/2), n.mass -1) * se.mass, 
         upper.ci.mass =mean.mass + qt(1-(0.05/2), n.mass -1) * se.mass) |> 
  ungroup()
## `summarise()` has grouped output by 'CLUTCH_NUMBER'. You can override using the
## `.groups` argument.
## Warning: There were 88 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.mass = mean.mass - qt(1 - (0.05/2), n.mass - 1) *
##   se.mass`.
## ℹ In group 1: `CLUTCH_NUMBER = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 87 remaining warnings.
ggplot(data = m2.mass.df, aes(x=MASS_MIDPOINT, y=emmean)) + 
  stat_smooth(aes(color=TEMPERATURE), 
              method = "lm", 
              formula = y ~ x) + 
  geom_pointrange(data = m2.mass.obs.summarize, aes(x =mean.mass.male, 
                                                  y =mean.mass, 
                                                  ymin =lower.ci.mass, 
                                                  ymax =upper.ci.mass, 
                                                  color = TEMPERATURE)) +  
  scale_color_manual(values = c("#69d7d8","#ff9c56", "#903146")) + 
  scale_fill_manual(values =c("#69d7d8","#ff9c56", "#903146")) +
  facet_wrap(~TEMPERATURE)+
  xlab("PARENTAL MIDPOINT STANDARD LENGTH (mm)") + 
  ylab("OFFSPRING STANDARD LENGTH (mm)") + 
  ggtitle("Offspring-male relationship") +
  theme_classic()
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 10 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 16 rows containing missing values or values outside the scale range
## (`geom_segment()`).

2.5-month

Models

Main hypothesis
model1a <- glmmTMB(MEDIAN_MASS ~ scale(MASS_MIDPOINT)*TEMPERATURE + scale(DENSITY), 
                    family=gaussian(link ="identity"), 
                    data=m2.5_df)
Alternative hypothesis
model1b <- glmmTMB(MEDIAN_MASS ~ MASS_MIDPOINT*TEMPERATURE*as.numeric(PARENTAL_DAYS_IN_TREATMENT) + scale(DENSITY), 
                    family=gaussian(link ="identity"), 
                    data=m2.5_df)

Model selection

AIC(model1a, model1b, k=12) 
BIC(model1a, model1b)

Once again the NULL model seems to outperform our hypothesis testing models. Let’s follow the steps that we conducted for 2-month data and appy a log transformation to our dataset to see if it improved the model.

Model validation

DHARMa
model1a |> 
  simulateResiduals(plot=TRUE)  

## Object of Class DHARMa with simulated residuals based on 250 simulations with refit = FALSE . See ?DHARMa::simulateResiduals for help. 
##  
## Scaled residual values: 0.992 0.748 0.768 0.12 0.644 0.964 0.752 0.88 0.9 0.556 0.76 0.936 0.644 0.508 0.308 0.952 0.72 0.748 0.4 0.784 ...
model1a |> testResiduals(plot=T) 

## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.070167, p-value = 0.9721
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0217, p-value = 0.88
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 0, observations = 48, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.00000000 0.07397279
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                                      0
## $uniformity
## 
##  Asymptotic one-sample Kolmogorov-Smirnov test
## 
## data:  simulationOutput$scaledResiduals
## D = 0.070167, p-value = 0.9721
## alternative hypothesis: two-sided
## 
## 
## $dispersion
## 
##  DHARMa nonparametric dispersion test via sd of residuals fitted vs.
##  simulated
## 
## data:  simulationOutput
## dispersion = 1.0217, p-value = 0.88
## alternative hypothesis: two.sided
## 
## 
## $outliers
## 
##  DHARMa outlier test based on exact binomial test with approximate
##  expectations
## 
## data:  simulationOutput
## outliers at both margin(s) = 0, observations = 48, p-value = 1
## alternative hypothesis: true probability of success is not equal to 0.007968127
## 95 percent confidence interval:
##  0.00000000 0.07397279
## sample estimates:
## frequency of outliers (expected: 0.00796812749003984 ) 
##                                                      0
performance
model1a |> 
  check_model(detrend=FALSE)
## `check_outliers()` does not yet support models of class `glmmTMB`.

Partial effect plots

model1a |> ggemmeans(~MASS_MIDPOINT|TEMPERATURE) |> 
  plot(add.data =FALSE) 

model1a |> ggemmeans(~TEMPERATURE) |> 
  plot(add.data =FALSE) 
## NOTE: Results may be misleading due to involvement in interactions

Model investigation

Summary
model1a |> summary()
##  Family: gaussian  ( identity )
## Formula:          
## MEDIAN_MASS ~ scale(MASS_MIDPOINT) * TEMPERATURE + scale(DENSITY)
## Data: m2.5_df
## 
##      AIC      BIC   logLik deviance df.resid 
##   -108.2    -93.3     62.1   -124.2       40 
## 
## 
## Dispersion estimate for gaussian family (sigma^2): 0.0044 
## 
## Conditional model:
##                                       Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                           0.474818   0.016758  28.334  < 2e-16 ***
## scale(MASS_MIDPOINT)                  0.022410   0.015057   1.488    0.137    
## TEMPERATURE28.5                      -0.022567   0.026313  -0.858    0.391    
## TEMPERATURE30                        -0.019369   0.023234  -0.834    0.404    
## scale(DENSITY)                       -0.072548   0.009812  -7.394 1.42e-13 ***
## scale(MASS_MIDPOINT):TEMPERATURE28.5 -0.036351   0.026549  -1.369    0.171    
## scale(MASS_MIDPOINT):TEMPERATURE30   -0.014608   0.023392  -0.624    0.532    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Anova
model1a |> Anova()
Confint
model1a |> confint()
##                                             2.5 %      97.5 %    Estimate
## (Intercept)                           0.441972967  0.50766224  0.47481760
## scale(MASS_MIDPOINT)                 -0.007100349  0.05192105  0.02241035
## TEMPERATURE28.5                      -0.074138950  0.02900582 -0.02256656
## TEMPERATURE30                        -0.064906106  0.02616898 -0.01936856
## scale(DENSITY)                       -0.091778279 -0.05331715 -0.07254771
## scale(MASS_MIDPOINT):TEMPERATURE28.5 -0.088386829  0.01568507 -0.03635088
## scale(MASS_MIDPOINT):TEMPERATURE30   -0.060454561  0.03123867 -0.01460795
r-squared
model1a |> r2_nakagawa()
## Random effect variances not available. Returned R2 does not account for random effects.
## # R2 for Mixed Models
## 
##   Conditional R2: NA
##      Marginal R2: 0.555

Summary figure

m2.5_df <- m2.5_df |> 
  drop_na(MASS_MIDPOINT)
m2.5.mass <- emmeans(model1a, ~ MASS_MIDPOINT*TEMPERATURE, 
                 at =list(MASS_MIDPOINT=seq(from =min(m2.5_df$MASS_MIDPOINT), to =max(m2.5_df$MASS_MIDPOINT), by=.25)))

m2.5.mass.df <- as.data.frame(m2.5.mass)

m2.5.mass.obs <- drop_na(m2.5_df, MASS_MIDPOINT, MEDIAN_MASS) |> 
  mutate(Pred =predict(model1a, re.form=NA, type ='response'), 
         Resid =residuals(model1a, type ='response'), 
         Fit =Pred+Resid) 

m2.5.mass.obs.summarize <-  m2.5.mass.obs |> 
  group_by(CLUTCH_NUMBER, TEMPERATURE) |> 
  summarise(mean.mass =mean(Fit, na.rm=TRUE), 
            mean.mass.male =mean(MASS_MIDPOINT, na.rm = TRUE), 
            sd.mass =sd(Fit, na.rm =TRUE), 
            n.mass = n()) |> 
  mutate(se.mass = sd.mass / sqrt(n.mass), 
         lower.ci.mass =mean.mass - qt(1-(0.05/2), n.mass -1) * se.mass, 
         upper.ci.mass =mean.mass + qt(1-(0.05/2), n.mass -1) * se.mass) |> 
  ungroup()
## `summarise()` has grouped output by 'CLUTCH_NUMBER'. You can override using the
## `.groups` argument.
## Warning: There were 96 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `lower.ci.mass = mean.mass - qt(1 - (0.05/2), n.mass - 1) *
##   se.mass`.
## ℹ In group 1: `CLUTCH_NUMBER = 38`.
## Caused by warning in `qt()`:
## ! NaNs produced
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 95 remaining warnings.
ggplot(data = m2.5.mass.df, aes(x=MASS_MIDPOINT, y=emmean)) + 
  stat_smooth(aes(color=TEMPERATURE), 
              method = "lm", 
              formula = y ~ x) + 
  geom_pointrange(data = m2.5.mass.obs.summarize, aes(x =mean.mass.male, 
                                                  y =mean.mass, 
                                                  ymin =lower.ci.mass, 
                                                  ymax =upper.ci.mass, 
                                                  color = TEMPERATURE)) +  
  scale_color_manual(values = c("#69d7d8","#ff9c56", "#903146")) + 
  scale_fill_manual(values =c("#69d7d8","#ff9c56", "#903146")) +
  facet_wrap(~TEMPERATURE)+
  xlab("PARENTAL MALE STANDARD LENGTH (mm)") + 
  ylab("OFFSPRING STANDARD LENGTH (mm)") + 
  ggtitle("Offspring-male relationship") +
  theme_classic()
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 12 rows containing missing values or values outside the scale range
## (`geom_segment()`).
## Warning: Removed 18 rows containing missing values or values outside the scale range
## (`geom_segment()`).